﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using System.Configuration;
using System.Data;
using System.Collections.Specialized;
using System.ServiceModel;

using BSC;
using EmrDatastructure;
using System.Data.Entity.Infrastructure;
using System.Data.Entity.Validation;
using EmrLogger;


namespace EmrDAL
{
    public partial class EmrService : IEmrService
    {
        public virtual void CreateEmrProblem(EmrProblem r)
        {
            try
            {
                using (var context = new EmrContext())
                {
                    context.Problems.Add(r);
                    context.SaveChanges();
                }
            }
            catch (Exception ex)
            {
                LogAndThrow.Log("Error CreateEmrProblem", ex);
            }
        }
        public virtual void UpdateEmrProblem(EmrProblem r)
        {
            try
            {
                using (var context = new EmrContext())
                {
                    context.Problems.Attach(r);
                    context.Entry(r).State = EntityState.Modified;
                    context.SaveChanges();
                }
            }
            catch (Exception ex)
            {
                LogAndThrow.Log("Error UpdateEmrProblem", ex);
            }
        }
        public virtual void DeleteEmrProblem(int id)
        {
            try
            {
                using (var context = new EmrContext())
                {
                    context.Problems.Remove(context.Problems.Find(id));
                    context.SaveChanges();
                }
            }
            catch (Exception ex)
            {
                LogAndThrow.Log("Error DeleteEmrProblem", ex);
            }
        }
        public virtual IList<EmrProblem> ReadListEmrProblem(string emrDemographicId)
        {
            try
            {
                using (var context = new EmrContext())
                {
                    return context.Problems.Where(p => p.EmrDemographicId == emrDemographicId).ToList();
                }
            }
            catch (Exception ex)
            {
                LogAndThrow.Log("Error ReadListEmrProblem", ex);
                return null;
            }
        }
        public virtual EmrProblem ReadEmrProblem(string id)
        {
            try
            {
                using (var context = new EmrContext())
                {
                    return context.Problems.Find(Convert.ToInt32(id));
                }
            }
            catch (Exception ex)
            {
                LogAndThrow.Log("Error ReadEmrProblem", ex);
                return null;
            }
        }

    }
}
